import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IEDA.
 * Description:
 * User:86186
 * Date:2023-12-09
 * Time:16:44
 */

/**
 * 回溯:17. 电话号码的字母组合
 * 给定一个仅包含数字 2-9 的字符串，返回所有它能表示的字母组合。答案可以按 任意顺序 返回。
 * 给出数字到字母的映射如下（与电话按键相同）。注意 1 不对应任何字母。
 * 示例 1：
 * 输入：digits = "23"
 * 输出：["ad","ae","af","bd","be","bf","cd","ce","cf"]
 */
public class letterCombinations {
    public static void main(String[] args) {
        String digits = "23";
        letterCombinations(digits);
    }
    static String[] hash = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
    static List<String> outcome;
    static StringBuffer path;
    public static List<String> letterCombinations(String digits) {
        outcome = new ArrayList<>();
        path = new StringBuffer();
        dfs(digits,0);
        return outcome;
    }

    private static void dfs(String digits, int pos) {
        if (pos == digits.length()){
            outcome.add(path.toString());
            return;
        }
        String cur = hash[digits.charAt(pos) - '0'];
        for (int i = 0; i < digits.length(); i++) {
            path.append(cur.charAt(i));

            dfs(digits,pos + 1);

            path.deleteCharAt(path.length() - 1);

        }
    }
}
